package com.tigercel.xiangchengyun.ui.fragments

import android.content.Intent
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.adapter.NewsPagerAdapter
import com.tigercel.xiangchengyun.ui.activities.NewsChannelActivity
import com.tigercel.xiangchengyun.utils.Global
import com.tigercel.xiangchengyun.utils.JsonUtil
import kotlinx.android.synthetic.main.fragment_video.*
import org.jetbrains.anko.support.v4.toast

class VideoFragment : Fragment() {

    private var containerView: View? = null
    val titles = mutableListOf<String>()
    val channelids = mutableListOf<String>()
    private var fragments: MutableList<Fragment>? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        containerView = inflater.inflate(R.layout.fragment_video, container, false)
        return containerView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        initNewsChannels()
        getChancles()
//        initFragments()
//        viewPager.adapter = NewsPagerAdapter(childFragmentManager,titles,fragments)
//        tabLayout.setupWithViewPager(viewPager)
//        TabLayoutUtil.dynamicSetTabLayoutMode(tabLayout)

        add_channel_iv.setOnClickListener({
            startActivity(Intent(activity, NewsChannelActivity::class.java))
        })
    }

    private fun initNewsChannels() {
//        titles = listOf("视频", "快视频", "新闻频道", "生活频道")
    }

    private fun initFragments() {
        fragments = ArrayList()
        for (i in 0 until channelids!!.size) {
            var fragment: Fragment = when (channelids[i].toInt()) {
                10 -> NewsCommonFragment.newInstance(channelids[i].toInt())!!
                11 -> VideoNormalFragment.newInstance(channelids[i].toInt())!!
                12 -> VideoSpecialFragment.newInstance(channelids[i].toInt())!!
                14 -> VideoParticular2Fragment.newInstance(channelids[i].toInt())!!
                else -> VideoParticular2Fragment.newInstance(channelids[i].toInt())!!
            }
            (fragments as ArrayList<Fragment>).add(fragment)
        }
    }

    private fun getChancles() {//获取频道使用
        OkGo.get<String>(Global.baseUrl + "v1/channel/format")
                .tag(this)
                .params("type", "2")
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        var content: String? = response!!.body()
                        val status = JsonUtil.get_key_string("status", content!!)
                        val msg = JsonUtil.get_key_string("msg", content!!)
                        val code = JsonUtil.get_key_string("code", content!!)
                        if (status == "0") {
                            val data = JsonUtil.getList("data", content!!)
                            for (i in 0 until data.size) {
                                var chanle_name = JsonUtil.get_key_string("name", data[i])
                                var chanle_id = JsonUtil.get_key_string("channel_id", data[i])
                                titles!!.add(chanle_name)
                                channelids.add(chanle_id)

                            }
                            initFragments()
                            viewPager_video.adapter = NewsPagerAdapter(childFragmentManager, titles, fragments)
                            tabLayout_video.setupWithViewPager(viewPager_video)
                            tabLayout_video.tabMode = TabLayout.MODE_SCROLLABLE
                        } else {
                            toast(getString(R.string.readFaile))
                        }
                    }
                })
    }
}
